package com.sky.controller.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 分类管理
 *
 * @Author mr.zhao
 * @Date 2025/5/16 20:52
 */
@RestController
@RequestMapping("/admin/category")
public class CategoryController {

	@Autowired
	private CategoryService categoryService;

	@PostMapping
	public Result add(@RequestBody CategoryDTO dto){
		categoryService.add(dto);
		return Result.success();
	}

	/**
	 * 分页查询分类列表
	 * @param dto
	 * @return
	 */
	@GetMapping("/page")
	public Result page(CategoryPageQueryDTO dto){
		PageResult list = categoryService.page(dto);
		return Result.success(list);
	}

	/**
	 * 删除分类
	 * @param id
	 * @return
	 */
	@DeleteMapping
	public Result delete(@RequestParam Integer id){
		categoryService.delete(id);
		return Result.success();
	}

	/**
	 * 更新分类
	 * @param dto
	 * @return
	 */
	@PutMapping
	public Result update(@RequestBody CategoryDTO dto){
		categoryService.update(dto);
		return Result.success();
	}

	/**
	 * 根据类型查询分类
	 * @param type
	 * @return
	 */
	@GetMapping("/list")
	public Result<List<Category>> list(Integer type){
		List<Category> list = categoryService.list(type);
		return Result.success(list);
	}

}
